package com.wtwd.wtwddevice.service.impl;

import com.wtwd.device.constant.CmdTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.device.util.StringUtils;
import com.wtwd.wtwddevice.model.entity.Device;
import com.wtwd.wtwddevice.mq.PushMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IDeviceStatusService;
import com.wtwd.wtwddevice.service.IRedisService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class DeviceSendServiceImpl implements IDeviceSendService {

    @Resource
    private PushMsgService msgService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IRedisService redisService;

    @Resource
    private IDeviceStatusService deviceStatusService;

    @Override
    public boolean sendMsgDevice(WTProtocol wtProtocol) {
        String imei = wtProtocol.getImei();
        if (StringUtils.isEmpty(imei)) {
            return false;
        }
        Device device = deviceService.getDeviceInfoByImei(imei);
        if (device == null) {
            return false;
        }
        MsgTypeEnum msgTypeEnum = MsgTypeEnum.getMsgTypeEnum(wtProtocol.getCmdName());
        boolean onlineCmd = msgTypeEnum.isOnline();
        if (onlineCmd) {//如果是在线指令则只有设备在线才发送
            boolean onlineState = deviceStatusService.getDeviceOnlineState(device.getId());
            if (!onlineState) {
                return false;
            }
        }
        wtProtocol.setTime(DateUtils.format(new Date(), DateUtils.dateFormat15));
        wtProtocol.setBodyLength(wtProtocol.getBody().length());
        wtProtocol.setMesType(CmdTypeEnum.SEND_REQUEST.getCmdType().toString());
        wtProtocol.setIccid(device.getIccid());
        if (StringUtils.isEmpty(wtProtocol.getNumber())) {
            wtProtocol.setNumber(generateNumber(imei));
        }
        msgService.pushToDevice(device.getHost(),
                MessageBase.builder().deviceKey(wtProtocol.getImei())
                        .wtProtocol(wtProtocol)
                        .build());
        return true;
    }

    @Override
    public String generateNumber(String deviceKey) {
        String increment = redisService.increment(SERIALNUMBER_KEY + deviceKey, SERIALNUMBER_MIN, SERIALNUMBER_EXPIRE, SERIALNUMBER_MAX);
        String format = String.format("%04d", Integer.valueOf(increment));
        return DateUtils.format(new Date(), DateUtils.dateFormat15).concat(format);
    }

    public static void main(String[] args) {
        System.out.println(String.format("%04d", 1111));
    }


    //插入或者更新心率频率设置值
//    Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
//    Frequency frequency = new Frequency();
//            frequency.setDeviceId(deviceId);
//            frequency.setNumber();
//            frequencyService.insertOrUpdate()
}
